package cn.christ.com.mobilesafe.view.activity_process.presenter;

import android.content.Context;
import android.content.pm.PackageManager;
import android.text.format.Formatter;

import java.util.ArrayList;
import java.util.List;

import cn.christ.com.mobilesafe.engine.ProcessProvider;
import cn.christ.com.mobilesafe.modle.ProcessInfo;
import cn.christ.com.mobilesafe.view.activity_process.ProcessManagerActivity;

/**
 * Created by Administrator on 2018/7/25.
 */

public class ProcessManagerPresenter implements IProcessManagerPresenter {

    List<ProcessInfo> SystemInfos = null;
    List<ProcessInfo> UserInfos = null;

    public ProcessManagerPresenter(Context context) {
        List<ProcessInfo> infos = null;
        SystemInfos = new ArrayList<>();
        UserInfos = new ArrayList<>();
        try {
            infos = ProcessProvider.getAllProcessInfos(context.getApplicationContext());
            for (ProcessInfo info : infos) {
                if (info.isSystem) {
                    SystemInfos.add(info);
                } else {
                    UserInfos.add(info);
                }
            }
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
    }

    @Override
    public List<ProcessInfo> getSysInfos() {
        return SystemInfos;
    }

    @Override
    public List<ProcessInfo> getUserInfos() {
        return UserInfos;
    }

    @Override
    public String getUsedMemory(ProcessManagerActivity processManagerActivity) {
        return Formatter.formatFileSize(
                processManagerActivity,
                ProcessProvider.getUsedMemory(processManagerActivity));
    }

    @Override
    public String getAllMemory(ProcessManagerActivity processManagerActivity) {
        return Formatter.formatFileSize(
                processManagerActivity,
                ProcessProvider.getAllMemory(processManagerActivity));
    }

    @Override
    public int getProgress(ProcessManagerActivity processManagerActivity) {
        return (int) (ProcessProvider.getUsedMemory(processManagerActivity) * 100
                / ProcessProvider.getAllMemory(processManagerActivity));
    }

}
